#!/usr/bin/env python3
"""
简单的CSVHeaderManager测试
"""

import csv
import os
import tempfile

def create_test_csv_without_header():
    """创建一个没有列名的测试CSV文件"""
    with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False, encoding='utf-8') as f:
        writer = csv.writer(f)
        # 写入数据行（没有列名）
        writer.writerow(['1', 'Alice', '25', 'Engineer'])
        writer.writerow(['2', 'Bob', '30', 'Teacher'])
        writer.writerow(['3', 'Charlie', '35', 'Doctor'])
        return f.name

def test_basic_functionality():
    """测试基本功能"""
    print("=== 测试CSVHeaderManager基本功能 ===")
    
    # 创建一个没有列名的测试CSV文件
    test_file = create_test_csv_without_header()
    print(f"创建测试文件: {test_file}")
    
    # 读取原始文件内容
    print("原始文件内容:")
    with open(test_file, 'r', encoding='utf-8') as f:
        content = f.read()
        print(content)
    
    # 创建输出文件
    output_file = "test_with_header.csv"
    
    # 手动模拟CSVHeaderManager的功能
    with open(test_file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        rows = list(reader)
    
    # 添加列名
    column_names = ["ID", "Name", "Age", "Occupation"]
    
    # 写入新的CSV文件
    with open(output_file, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(column_names)
        for row in rows:
            writer.writerow(row)
    
    print("处理后的文件内容:")
    with open(output_file, 'r', encoding='utf-8') as f:
        content = f.read()
        print(content)
    
    # 清理文件
    os.remove(test_file)
    os.remove(output_file)
    
    print("=== 测试完成 ===")

if __name__ == "__main__":
    test_basic_functionality()